package pro.syj.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pro.syj.bean.Course;
import pro.syj.db.DBassist;

public class CourseDao {
	/*
	 * 查看所有课程信息
	 * return ArrayList<Course>
	 */
	public ArrayList<Course> selectAll(){
		
		ArrayList<Course> courselist = new ArrayList<Course>();
		String sql = "select * from course";
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql);
			while(rss != null && rss.next()){
				Course course = new Course();
				course.setCid(rss.getString(1));
				course.setCname(rss.getString(2));
				course.setCategory(rss.getString(3));
				course.setPercent1(rss.getDouble(4));
				course.setPercent2(rss.getDouble(5));
				courselist.add(course);
			}
		}catch(Exception e){
				e.printStackTrace();
		}
		
		return courselist;
	}
	
	/*
	 * 查看某一门课程
	 * param cid
	 * return ArrayList<Course>
	 */
	public ArrayList<Course> selectOne(String cid){
		
		String sql = "select * from course where cid = ?";
		ArrayList<Course> courselist = new ArrayList<Course>();
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql, cid);
			if(rss != null && rss.next()){
				Course course = new Course();
				course.setCid(rss.getString(1));
				course.setCname(rss.getString(2));
				course.setCategory(rss.getString(3));
				course.setPercent1(rss.getDouble(4));
				course.setPercent2(rss.getDouble(5));
				courselist.add(course);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return courselist;
	}
	
	public Course selectOne2(String cid){
		
		String sql = "select * from course where cid = ?";
		Course course = new Course();
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql, cid);
			if(rss != null && rss.next()){
				course.setCid(rss.getString(1));
				course.setCname(rss.getString(2));
				course.setCategory(rss.getString(3));
				course.setPercent1(rss.getDouble(4));
				course.setPercent2(rss.getDouble(5));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return course;
	}
	/*
	 * 删除一门课程
	 * param cid
	 * return int
	 */
	public int deleteCourse(String cid){
		
		String sql = "delete from course where cid = ?";
		int flag = -1;
		
		try{
			DBassist dbs = new DBassist();
			flag = dbs.update(sql, cid);
			return flag;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}
	
	/*
	 * (管理员)修改某一门课程的成绩比例
	 * param cid p1 p2
	 * return int
	 */
	public int updateCourse(String cid,double p1,double p2){
		
		String sql = "update course set percent1 = ? ,percent2 = ? where cid = ?";
		int flag = -1;
		
		try{
			DBassist dbs = new DBassist();
			flag = dbs.update(sql, p1,p2,cid);
			return flag;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}
	
	/*
	 * 增加一门课程
	 * param cid cname category percent1 percent2
	 * return int
	 */
	public int addCourse(String cid,String cname,String category,double percent1,double percent2){
		
		String sql ="insert into course value(?,?,?,?,?)";
		int flag = -1;
		
		try{
			DBassist dbs = new DBassist();
			flag = dbs.update(sql, cid,cname,category,percent1,percent2);
			return flag;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}
	
	/*
	 * 根据cid查看这门课程是否存在
	 * return int
	 */
	public int isValid(String cid){
		
		String sql = "select * from course where cid = ?";
		
		try{
			DBassist dbs = new DBassist();
			ResultSet rss = dbs.query(sql, cid);
			if(rss != null){
				return 1;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return -1;
	}
	
}


























